<?php
namespace app\travian\controller;

use think\Controller;
use think\Request;
use think\Db;
use org\lucn\phpquery;
/**
 * travian控制器
 * @author  Lucn30 <Lucn30@126.com>
 */
class Tr44 extends Controller {
	//protected $server_url = "http://ts6.travian.tw";
	public function index(){
		$uid = input('?uid') ? input('uid') : 1;
		
		$user = DB::name('user')->select();
		foreach($user as $o => $son){
			if($uid == $son['id']){
				$me = $son;
			}
		}
		$this->assign('user',$user);
		$this->assign('me',$me);
		$where['uid'] = $uid;
		
		$list = DB::name('viliage')->where($where)->order('id asc')->/*cache(true)->*/select();
		foreach($list as $i => $son){
			$list[$i]['date'] = date('m-d H:i:s',$son['start_build_time']);
			if(time() > $son['start_build_time']){
				$list[$i]['start'] = 'ok';
			}else{
				$list[$i]['start'] = 'no';
			}
			//build_task
			$where2['vid'] = $son['vid'];
			$list[$i]['task'] = DB::name('build_task')->where($where2)->order('id asc')->/*limit(5)->*//*cache(true)->*/select();
			//desbuild_task
			$where4['vid'] = $son['vid'];
			$list[$i]['task_chai'] = [];
			if(time()>$son['destory_time']){
			    $list[$i]['task_chai'] = DB::name('desbuild_task')->where($where2)->order('id asc')->/*limit(5)->*//*cache(true)->*/select();
			}
			
			//造兵_task
			$where3['vid'] = $son['vid'];
			//$where3['start_time'] = array('lt',time());
			//$where3['ts'] = array('gt',0);
			//$where3['ts2'] = array('gt',0);
			$one_zaobing = DB::name('zaobing_task')->where($where3)->/*where('ts|ts2','gt',0)->*//*limit(5)->*//*cache(true)->*/find();
			//dump($one_zaobing);die;
			//if($one_zaobing){
				//if(time()>$one_zaobing['start_time'] && $one_zaobing['ts']>0 && $one_zaobing['ts2']>0){
					$list[$i]['task_b'] = $one_zaobing;
				//}
				
			//}
			
			//dump($list);die;
		}
		//dump($list);die;
		$arr_arg = explode('|',$me['server_arg']);
		$arr_node1 = explode(',',$arr_arg[2]);
		$this->assign('list',$list);
		$this->assign('uid',$uid);
		$this->assign('dorf',$arr_arg[0]);
		$this->assign('dorf3',$arr_arg[1]);
		$this->assign('node1',$arr_node1[0]);//选择器每个村庄
		$this->assign('node2',$arr_node1[1]);//选择器每个村庄的链接
		$this->assign('newdid','newdid');
		//$this->assign('list2',$list2);
		return $this->Fetch('/index_tr44');
    }
	
		public function update_v(){
	    $uid = input('get.uid');
		$str = input('get.str');
		$strname = input('get.strname');
		$arr_v = explode(',',$str);
		$arr_vname = explode(',',$strname);
		$where['uid'] = $uid;
		$where['vid'] = array('IN',$str);
		$a = 0;
		$r = DB::name('viliage')->where($where)->field('id,vid')->select();
		    foreach($r as $i => $son){
			   if(!in_array($son['vid'], $arr_v)) {
			       $del = DB::name('viliage')->where('id',$son['id'])->delete();
				   $a = 1;
			   }
			}
			$where2['uid'] = $uid;
			foreach($arr_v as $i => $son){
			   $where2['vid'] = $son;
			   $r = DB::name('viliage')->where($where2)->find();
			   $data['uid'] = $uid;
			   $data['vid'] = $son;
			   $data['vname'] = $arr_vname[$i];
			   if($r){
			       $upd = DB::name('viliage')->where('id',$r['id'])->update($data);
				   if($upd){
				       $a = 1;
				   }
			   }else{
			       $ins = DB::name('viliage')->insert($data);
				   $a = 1;
			   }
			}
			
		/*
		$arr = explode(',',$str);
		$where['uid'] = $uid;
		foreach($arr as $i => $son){
		    $where['vid'] = $son;
			$r = DB::name('viliage')->where($where)->find();
			if(!r){
			    
			}
		}
		*/
	return json($a);
	}
	
	public function attack_list(){
		$where = array();
		if(input('?vid'))$where['vid'] = input('vid');
		$list = DB::name('attack_task')->where($where)->order('vid asc,id asc')->select();
		if(input('?vid'))$this->assign('vid',input('vid'));
		$this->assign('list',$list);
		return $this->Fetch('/index_attack_list');
	}
	
	public function attack_del(){
		$del = DB::name('attack_task')->delete(input('id'));
		return json($del);
	}
	
	public function attack_edit(){
		$data = input('post.');
		unset($data['id']);
		$where['id'] = input('id');
		$upd = DB::name('attack_task')->where($where)->update($data);
		return json($upd);
	}
	
	public function get_viliage(){
		$uid = input('account');
		$where['uid'] = $uid;
		$list = DB::name('viliage')->where($where)->/*cache(true)->*/select();
		foreach($list as $i => $son){
			$list[$i]['date'] = date('m-d H:i:s',$son['start_build_time']);
			if(time() > $son['start_build_time']){
				$list[$i]['start'] = 'ok';
			}else{
				$list[$i]['start'] = 'no';
			}
			//build_task
			$where2['vid'] = $son['vid'];
			$list2 = DB::name('build_task')->where($where2)->order('id asc')->order('id asc')->/*cache(true)->*/select();
			
		}
		$this->assign('list',$list);
		$this->assign('list2',$list2);
		//$this->assign('viliages',$list);
		//return json($list);
	}
	
	public function get_task(){
		$uid = input('uid');
		//取账号信息
		$where_u['id'] = $uid;
		$one_user = DB::name('user')->where($where_u)->cache(true)->find();
		
		$where['uid'] = $uid;
		$task = array();
		if($one_user['tribe'] == 'Romanos'){
			$list = DB::name('viliage')->where($where)->where('start_build_time|start_build_time2','lt',time())->select();
		}else{
		    $list = DB::name('viliage')->where($where)->where('start_build_time','lt',time())->select();
		}
		//
		
		foreach($list as $i => $son){
			$list[$i]['date1'] = date('Y-m-d H:i:s',$son['start_build_time']);
			$list[$i]['date2'] = date('Y-m-d H:i:s',$son['start_build_time2']);
			$where2['vid'] = $son['vid'];
			//$where3['vid'] = $son['vid'];
			if($one_user['tribe'] == 'Romanos'){
			    if($son['start_build_time']<time()){
			        $where2['build'] = array('lt',19);
					$taskone = DB::name('build_task')->where($where2)->order('id asc')->find();
					if($taskone){$task[] = $taskone;}
			    }
				if($son['start_build_time2']<time()){
				    $where2['build'] = array('gt',18);	
					$taskone = DB::name('build_task')->where($where2)->order('id asc')->find();
					if($taskone){$task[] = $taskone;}
				}
			}else{
				$taskone = DB::name('build_task')->where($where2)->order('id asc')->find();
				if($taskone){$task[] = $taskone;}
				}
		 }
		//
		//dump($list);die;
		//dump($task);die;
		//$this->assign('viliages',$list);
		return json($task);
	}
	
	public function get_task2(){
		$uid = input('uid');
		$where['uid'] = $uid;
		$list = DB::name('viliage')->where($where)->select();
		$task = array();
		
		foreach($list as $i => $son){
			$where3['vid'] = $son['vid'];
			$where3['start_time'] = array('lt',time());
			//$where3['ts'] = array('gt',0);
			//$where3['ts2'] = array('gt',0);
			$taskone = DB::name('zaobing_task')->where($where3)->where('ts|ts2','gt',0)->/*limit(5)->*//*cache(true)->*/find();
			if($taskone){
				$task[] = $taskone;
			}
		}
		//$this->assign('viliages',$list);
		return json($task);
	}
	
	public function get_task3(){
		$uid = input('uid');
		$where['uid'] = $uid;
		$list = DB::name('viliage')->where($where)->select();
		$task = array();
		
		foreach($list as $i => $son){
			$where3['vid'] = $son['vid'];
			$where3['start_time'] = array('lt',time());
			$where3['status'] = array('eq',1);
			//$where3['ts'] = array('gt',0);
			//$where3['ts2'] = array('gt',0);
			$task_list = DB::name('attack_task')->where($where3)->/*limit(5)->*//*cache(true)->*/select();
			foreach($task_list as $i2 => $son2){
				$task[] = $son2;
			}
		}
		//$this->assign('viliages',$list);
		return json($task);
	}
	
	public function get_task_chai(){
		$uid = input('uid');
		$where['uid'] = $uid;
		$where['destory_time'] = array('lt',time());
		$list = DB::name('viliage')->where($where)->select();
		$task = array();
		
		foreach($list as $i => $son){
			$where3['vid'] = $son['vid'];
			//$where3['ts'] = array('gt',0);
			//$where3['ts2'] = array('gt',0);
			$task_list = DB::name('desbuild_task')->where($where3)->/*limit(5)->*//*cache(true)->*/select();
			foreach($task_list as $i2 => $son2){
				$task[] = $son2;
			}
		}
		//$this->assign('viliages',$list);
		return json($task);
	}
	
	public function set_task(){
		$vid = input('vid');
		$id = input('id');
		$where_u['id'] = input('uid');
		$one_user = DB::name('user')->where($where_u)->cache(true)->find();
		$inctime = input('inctime');
		$where['uid'] = input('uid');
		$nono = input('nono');
		$where['vid'] = $vid;
		$where2['id'] = $id;
		if($one_user['tribe'] == 'Romanos'){
		    $one_b = DB::name('build_task')->where($where2)->find();
			if($one_b['build']<=18){
			    $r = DB::name('viliage')->where($where)->setField('start_build_time',$inctime+time());
			}else{
			    $r = DB::name('viliage')->where($where)->setField('start_build_time2',$inctime+time());
			}
		}else{
		    $r = DB::name('viliage')->where($where)->setField('start_build_time',$inctime+time());
		}
		
		if($nono != 'nono'){$del = DB::name('build_task')->where($where2)->delete();}else{$del = 1;}
		//$del = 'oo';
		if($r && $del){
			$return = 'ok';
		}else{
			$return = 'no';
		}
		return $return;
	}
	
	public function set_task2(){
		$id = input('id');
		$times = input('times');
		$where['id'] = $id;
		$r = DB::name('zaobing_task')->where($where)->setField('start_time',$times+time());
		//$del = 'oo';
		if($r){
			$return = 'ok';
		}else{
			$return = 'no';
		}
		return $return;
	}
	
	public function set_task3(){
		$id = input('id');
		$times = input('times');
		$where['id'] = $id;
		$r = DB::name('attack_task')->where($where)->setField('start_time',$times+time());
		//$del = 'oo';
		if($r){
			$return = 'ok';
		}else{
			$return = 'no';
		}
		return $return;
	}
	
	public function set_task_chai(){
		$vid = input('vid');
		$id = input('id');
		$inctime = input('inctime');
		$where['uid'] = input('uid');
		$nono = input('nono');
		$where['vid'] = $vid;
		$where2['id'] = $id;
		$r = DB::name('viliage')->where($where)->setField('destory_time',$inctime+time());
		if($nono != 'nono'){$del = DB::name('desbuild_task')->where($where2)->delete();}else{$del = 1;}
		//$del = 'oo';
		if($r && $del){
			$return = 'ok';
		}else{
			$return = 'no';
		}
		return $return;
	}
	
	public function set_zaobing(){
		$vid = input('vid');
		$data = input('post.');
		$data['vid'] = $vid;
		//看看是否存在
		$where['vid'] = $vid;
		$one = DB::name('zaobing_task')->where($where)->find();
		if($one){
			$r = DB::name('zaobing_task')->where($where)->update($data);
		}else{
			$r = DB::name('zaobing_task')->insert($data);
		}
		//$del = 'oo';
		return $r;
	}
	
	public function add_build(){
		$data['vid'] = input('vid');
		$data['build'] = input('bid');
		$data['title'] = input('title');
		$data['level'] = input('?level') ? input('level') : 0;
		$id = Db::name('build_task')->insertGetId($data);
		if($id){
			$data['res'] = 1;
			$data['id'] = $id;
		}else{
			$data['res'] = 0;
		}
		return json($data);
	}
	
	public function add_build_chai(){
		$data['vid'] = input('vid');
		$data['build'] = input('bid');
		$data['title'] = input('title');
		//$data['level'] = input('?level') ? input('level') : 0;
		$id = Db::name('desbuild_task')->insertGetId($data);
		if($id){
			$data['res'] = 1;
			$data['id'] = $id;
		}else{
			$data['res'] = 0;
		}
		return json($data);
	}
	
	public function del_build(){
		$id= input('id');
		$id = Db::name('build_task')->delete($id);
		if($id){
			$data['res'] = 1;
			$data['id'] = $id;
		}else{
			$data['res'] = 0;
		}
		return json($data);
	}
	
	public function del_build_chai(){
		$id= input('id');
		$id = Db::name('desbuild_task')->delete($id);
		if($id){
			$data['res'] = 1;
			$data['id'] = $id;
		}else{
			$data['res'] = 0;
		}
		return json($data);
	}
	
	public function get_taskfirst(){
		$vid = input('vid');
		$where['vid'] = $vid;
		$one = DB::name('build_task')->where($where)->order('id desc')->/*cache(true)->*/find();
		//$this->assign('viliages',$list);
		return json($one);
	}
	
	public function set_build_time(){
		$uid = input('uid');
		$vid = input('vid');
		$bid = input('bid');
		$s = input('s');
		$where['uid'] = $uid;
		$where['vid'] = $vid;
		$del = DB::name('build_task')->/*cache(true)->*/delete($bid);
		$one = DB::name('viliage')->where($where)->/*cache(true)->*/setInc('start_build_time', $s);
		//$this->assign('viliages',$list);
		return json($one);
	}
	
	public function add_attack(){
		$data['vid'] = input('vid');
		$data['x'] = input('form1_x');
		$data['y'] = input('form1_y');
		$data['times'] = input('form1_s');
		$data['note'] = input('form1_note');
		$t1  = is_numeric(input('form1_t1')) ? input('form1_t1') : 0;
		$t2  = is_numeric(input('form1_t2')) ? input('form1_t2') : 0;
		$t3  = is_numeric(input('form1_t3')) ? input('form1_t3') : 0;
		$t4  = is_numeric(input('form1_t4')) ? input('form1_t4') : 0;
		$t5  = is_numeric(input('form1_t5')) ? input('form1_t5') : 0;
		$t6  = is_numeric(input('form1_t6')) ? input('form1_t6') : 0;
		$t7  = is_numeric(input('form1_t7')) ? input('form1_t7') : 0;
		$t8  = is_numeric(input('form1_t8')) ? input('form1_t8') : 0;
		$t9  = is_numeric(input('form1_t9')) ? input('form1_t9') : 0;
		$t10 = is_numeric(input('form1_t10')) ? input('form1_t10') : 0;
		$t11 = is_numeric(input('form1_t11')) ? input('form1_t11') : 0;
		$data['t']= $t1.'|'.$t2.'|'.$t3.'|'.$t4.'|'.$t5.'|'.$t6.'|'.$t7.'|'.$t8.'|'.$t9.'|'.$t10.'|'.$t11.'|';
		$data['status'] = 1;
		$data['start_time'] = 0;
		$where['vid'] = input('vid');
		$where['x'] = $data['x'];
		$where['y'] = $data['y'];
		$one = DB::name('attack_task')->where($where)->find();
		if($one){
			$a['a'] = 0;$a['b'] = '坐标已存在';
		}else{
		    $a['a'] = DB::name('attack_task')->/*cache(true)->*/insert($data);
			$a['b'] = '添加目标成功';
		}
		//$this->assign('viliages',$list);
		return json($a);
	}
	
    public function index1(){
		phpQuery::newDocumentFile($this->server_url.'/dorf1.php'); 
		echo pq(".blkTop h1:eq(0)")->html(); 
    }
	
	public function show(){
        return $this->Fetch('/article_show');
    }
	
	public function zaobing(){
        return $this->Fetch('/index_zaobing');
    }
}